package com.liang.book.aspect;

import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

@Component
@Slf4j
@Aspect
public class TimeRecordAspect {//若语法错误不会报错，也不会生效
    @Around("execution (* com.liang.book.controller.*.*(..))") //目标方法前后都执行，必须有返回结果
    public Object timeRecord(ProceedingJoinPoint joinPoint) throws Throwable {   //只有@Around可以用ProceedingJoinPoint joinPoint
        //1.记录开始时间
        //2.执行目标方法
        //3.记录结束时间
        //4.记录耗时
        long start = System.currentTimeMillis();
        log.info("do Around 前");
        Object result = joinPoint.proceed();
        long end = System.currentTimeMillis();
        log.info(joinPoint.getSignature()+"耗时：" +(end-start) +"ms");
        return result;
    }
    @Before("execution (* com.liang.book.controller.*.*(..))")
    public void doBefore(JoinPoint joinPoint) {  //JoinPoint joinPoint是连接点，可以利用它来获取连接点信息
        log.info(joinPoint.getSignature().toShortString());
        log.info("执行before方法");
    }
    @AfterThrowing("execution (* com.liang.book.controller.*.*(..))")
    public void doAfterThrowing() {
        log.info("执行doAfterThrowing方法");
    }
    @After("execution (* com.liang.book.controller.*.*(..))")
    public void doAfter() {
        log.info("执行after方法");
    }
    @AfterReturning("execution (* com.liang.book.controller.*.*(..))")
    public void doAfterReturing() {
        log.info("执行doAfterReturing方法");
    }


}
